//app.js
var key   = 'shsytour.cn'
var sign  = require('/utils/sign.js')
var token = '$token'
var user_sign  = '$sign'

App({
  onLaunch: function () {
    this.getUserInfo()
    if (!wx.getStorageSync('$uuid')) {
      this.request('wxapp/uuid', 'GET', {}, {}, function (res) {
        if (res.data.code == 200) {
          wx.setStorageSync('$uuid', res.data.data)
        } else { }
      })
    }
  },
  onShow: function (options) {
    var that = this
    wx.connectSocket({
      url: this.globalData.wss_url,
      data: {a:1}
    })

    wx.onSocketOpen(function (res) {
      if (that.checkLogin()) {
        var d = { 'sign': wx.getStorageSync(user_sign) }
        d = JSON.stringify(d)
        wx.sendSocketMessage({
          data: d,
        })
      }
    })

    wx.onSocketMessage(function (res) {
      that.globalData.c = res.data
    })
  },
  //get获取数据
  getJson: function (url, data = {}, cb) {
    this.request(url, 'GET', data, { 'sign': sign.sign(data, key), 'token': wx.getStorageSync('$token') }, cb)
  },
  //post提交数据
  post: function (url, data = {}, cb) {
    this.request(url, 'POST', data, { 'sign': sign.sign(data, key), 'token': wx.getStorageSync('$token') }, cb)
  },
  //
  request: function (url, method = 'GET', data = {}, header = {}, cb) {
    wx.showLoading({ title: '加载中...' })
    wx.request({
      url: this.globalData.api_url + url,
      data: data,
      method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: header, // 设置请求的 header
      success: function (res) {
        typeof cb == "function" && cb(res)
      },
      fail: function () {
        // fail
      },
      complete: function () {
        wx.hideLoading()
        // complete
      }
    })

  },
  checkLogin: function () {
    if (wx.getStorageSync(token)) {
      return true
    }
    return false
  },
  getCarAllTypes: function () {
    this.getJson('/wxapp/caravan/caravan_all_types', {}, function (res) {
      wx.setStorageSync('$caralltypes', res.data)
    })
  },
  getUserInfo: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      wx.setStorageSync('$user', this.globalData.userInfo)
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        success: function (login) {
          wx.getUserInfo({
            success: function (info) {
              if (login.code) {
                //发起网络请求
                that.getJson('wxapp/user/wxsession', { code: login.code, iv: info.iv, decryptData: info.encryptedData }, function (res) {
                  that.globalData.userInfo = res.data
                  wx.setStorageSync('$user', that.globalData.userInfo)
                  console.log(res.data.openId)
                }
                )
              } else {
                console.log(login)
              }
              typeof cb == "function" && cb(that.globalData.userInfo)
            },
            fail: function () {
              console.log('用户未授权')
            }
          })
        }
      })
    }
  },
  confirm: function () {
    this.setData({
      'dialog.hidden': true,
      'dialog.title': '',
      'dialog.content': ''
    })
  },
  globalData: {
    api_url: 'https://api.shsytour.cn/',
    wss_url: 'wss://wss.shsytour.cn/wss',
    c: 0,
    userInfo: null,
    loginUserInfo: null,
    latitude: null,
    temp: null
  }
})